<script setup lang="ts">
import { reactive, PropType, watch } from 'vue'
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'

const props = defineProps({
  data: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  }
})

const schema = reactive<FormSchema[]>([
  {
    field: 'name',
    label: '标签名称',
    component: 'Input',
    colProps: { span: 24 }
  },
  {
    field: 'imgUrl',
    label: '规格类目图片',
    component: 'Upload'
  },
  {
    field: 'previewImgUrl',
    label: '预览显示图片',
    component: 'Upload'
  }
])
const { register, methods, elFormRef } = useForm({
  schema
})
const { setValues } = methods
setValues({})

watch(
  () => props.data,
  (value) => {
    if (!value) return
    const { setValues } = methods
    setValues(value)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>
<template>
  <Form @register="register" />
</template>
